/**
 * OA项目模块统一导入配置文件
 * 使用静态导入路径，避免动态require
 */

const pathResolver = require('./path_resolver.js');

// 直接使用备用实现的resourcesStore，不再尝试动态加载可能不存在的模块
const resourcesStore = {
  CACHE_GROUP: {
    CACHE_KEY: 'RESOURCES_CACHE_KEY',
    CACHE_TIME: 30 * 60 * 1000
  },
  error: null
};
console.log('[path_import.js] 使用内置的resourcesStore默认配置');

// 导入npm配置模块
const npmConfig = require('./npm_config.js');

// 静态导入globals模块 - 使用静态路径
// 页面上下文(Page)
let pageHelperPage, cloudHelperPage, timeHelperPage;
// 服务上下文(Service)
let pageHelperService, cloudHelperService, timeHelperService;

try {
  // 尝试导入页面上下文模块
  pageHelperPage = require('../../../helpers/page_helper.js');
  cloudHelperPage = require('../../../helpers/cloud_helper.js');
  timeHelperPage = require('../../../helpers/time_helper.js');
  
  // 尝试导入服务上下文模块
  pageHelperService = require('../../../helpers/page_helper.js');
  cloudHelperService = require('../../../helpers/cloud_helper.js');
  timeHelperService = require('../../../helpers/time_helper.js');
  
  console.log('[path_import.js] 成功加载基础辅助模块');
} catch (err) {
  console.error('[path_import.js] 加载基础辅助模块失败:', err);
  
  // 提供基础的兜底实现
  pageHelperPage = pageHelperService = {
    url: () => {},
    showModal: (msg) => { console.error(msg); }
  };
  cloudHelperPage = cloudHelperService = {
    callCloud: async () => ({})
  };
  timeHelperPage = timeHelperService = {
    time: () => new Date().toISOString()
  };
}

// 直接从项目内部导入ProjectBiz
let ProjectBiz;
try {
  ProjectBiz = require('../comm/biz/project_biz.js');
  console.log('[path_import.js] 成功导入ProjectBiz模块');
} catch (err) {
  console.error('[path_import.js] 导入ProjectBiz失败，使用默认实现', err);
  // 提供基础的兜底实现
  ProjectBiz = {
    initPage: function(that) {
      console.log('ProjectBiz.initPage被调用', that);
      // 基本初始化
      if (that && that.setData) {
        that.setData({
          isInit: true
        });
      }
    },
    isValid: function(id) {
      return !!id;
    }
  };
}

/**
 * 获取全局辅助模块
 * @param {string} context - 调用上下文，可选值：'service', 'page'
 * @returns {object} 全局辅助模块配置
 */
function getGlobalHelpers(context = 'page') {
  try {
    // 根据不同的上下文返回对应的辅助模块
    if (context === 'page') {
      return {
        pageHelper: pageHelperPage,
        cloudHelper: cloudHelperPage,
        timeHelper: timeHelperPage,
        ProjectBiz: ProjectBiz, // 添加ProjectBiz导出
        validate: { checkRequired: () => '' },
        fileHelper: { getExtByMime: () => 'unknown' },
        routerHelper: { navigateTo: () => {} },
        
        // NPM包
        mobx: npmConfig.mobx,
        createStoreBindings: npmConfig.createStoreBindings,
        storeBindingsBehavior: npmConfig.storeBindingsBehavior,
        dayjs: npmConfig.dayjs
      };
    } else {
      // 服务上下文
      return {
        pageHelper: pageHelperService,
        cloudHelper: cloudHelperService,
        timeHelper: timeHelperService,
        ProjectBiz: ProjectBiz, // 添加ProjectBiz导出
        validate: { checkRequired: () => '' },
        fileHelper: { getExtByMime: () => 'unknown' },
        routerHelper: { navigateTo: () => {} },
        
        // NPM包
        mobx: npmConfig.mobx,
        createStoreBindings: npmConfig.createStoreBindings,
        storeBindingsBehavior: npmConfig.storeBindingsBehavior,
        dayjs: npmConfig.dayjs
      };
    }
  } catch (err) {
    console.error('[path_import.js] 加载全局模块配置失败', err);
    
    // 提供备用方案 - 使用已导入的静态模块
    return {
      pageHelper: context === 'page' ? pageHelperPage : pageHelperService,
      cloudHelper: context === 'page' ? cloudHelperPage : cloudHelperService,
      timeHelper: context === 'page' ? timeHelperPage : timeHelperService,
      ProjectBiz: ProjectBiz, // 添加ProjectBiz导出
      validate: {
        checkRequired: (val, desc = '') => val ? '' : `${desc}不能为空`
      },
      routerHelper: {
        navigateTo: (url) => wx.navigateTo({ url })
      }
    };
  }
}

module.exports = {
  getGlobalHelpers,
  pathResolver,
  // 直接从npmConfig导出关键模块，方便直接使用
  createStoreBindings: npmConfig.createStoreBindings,
  mobx: npmConfig.mobx,
  dayjs: npmConfig.dayjs,
  // 导出全局Store
  resourcesStore: resourcesStore,
  // 直接导出ProjectBiz
  ProjectBiz: ProjectBiz
}; 